import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../views/Home.vue'
import User from '../views/User.vue'
import Manage from '../components/admin/Manage.vue'
import Buy from '../components/admin/Buy.vue'
import Wxiu from '../components/admin/Wxiu.vue'
import Bfei from '../components/admin/Bfei.vue'
import Welcome from '../components/Welcome.vue'
import Pwchange from '../components/user/Pwchange.vue'
Vue.use(VueRouter)

const routes = [

  {
    path: '/login',
    name: 'login', 
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    redirect: '/admin/welcome',
    children: [
      { path: '/admin/welcome', component:Welcome},
      { path: '/admin/manage', component:Manage},
      { path: '/admin/buy', component:Buy},
      { path: '/admin/wxiu', component:Wxiu},
      { path: '/admin/bfei', component:Bfei}
    ]
  },
  {
    path: '/user',
    name: 'user',
    component: User,
    redirect: '/user/welcome',
    children: [
      { path: '/user/welcome', component:Welcome},
      { path: '/user/buy', component:Buy},
      { path: '/user/wxiu', component:Wxiu},
      { path: '/user/bfei', component:Bfei},
      { path: '/user/pwchange', component:Pwchange}
    ]
  },
  {
    path: '/',
    name: 'Login',
    component: Login
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

  router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    const tokenstr = window.sessionStorage.getItem('token')
    if (!tokenstr) return next('/login')
    next() 
  })

export default router
